package leetcode;

import util.Tree;
import util.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0257.java
 * @Author : WangRuoyu
 * @Date 2023/11/11 20:41
 */


public class Q0257 {
    List<String> tmp = new ArrayList<>();

    List<String> res = new ArrayList<>();

    public void dfs(TreeNode node) {
        if (node.left == null && node.right == null) {
            tmp.add(String.valueOf(node.val));
            res.add(String.join("->", tmp));
            tmp.remove(tmp.size() - 1);
        }
        if (node.left != null) {
            tmp.add(String.valueOf(node.val));
            dfs(node.left);
            tmp.remove(tmp.size() - 1);
        }
        if (node.right != null) {
            tmp.add(String.valueOf(node.val));
            dfs(node.right);
            tmp.remove(tmp.size() - 1);
        }
    }

    public List<String> binaryTreePaths(TreeNode root) {
        dfs(root);
        return res;
    }

    public static void main(String[] args) {
        Q0257 sl = new Q0257();
        String[] tree = {"1", "2", null, "5", null, null, "3", null, null};
        TreeNode root = Tree.createTreeByPreorder(tree);
        System.out.println(sl.binaryTreePaths(root));
    }
}